package com.lysj.bhp.wallet.util;

import com.lysj.bhp.util.json.JacksonUtil;
import info.blockchain.api.APIException;
import info.blockchain.api.HttpClient;
import info.blockchain.api.wallet.Wallet;
import info.blockchain.api.wallet.entity.Address;
import info.blockchain.api.wallet.entity.PaymentResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class BlockChainUtil {
    private final static String API_SERVICE_URL = "http://localhost:3000/";
    private final static String API_CODE = "ee517561-8e49-4c99-97ab-eaa90aea56bd";

    private final static String EMAIL_WALLET = "btc@bhpcash.io";

    private final static Long BITCOIN_FEE = 1000000L;

    /**
     * 创建一个比特币钱包
     *
     * @param password
     * @param email
     * @return
     */
    public static Map<String, String> createWallet(String password, String email) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("api_code", API_CODE);
        paramMap.put("password", password);
        paramMap.put("label", "Main Address");
//        paramMap.put("email", email);
        paramMap.put("hd", "true");
        String response = null;
        try {
            response = HttpClient.getInstance().post(API_SERVICE_URL, "api/v2/create", paramMap);
        } catch (APIException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(response);
        return JacksonUtil.toStringMap(response);
    }

    /**
     * 创建一个比特币钱包地址
     */
    public static Map<String, String> createAddress(String guid, String password, String label, String secondPassword) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("api_code", API_CODE);
        paramMap.put("password", password);
        paramMap.put("label", label);
//        paramMap.put("secondPassword", secondPassword);
        String response = null;
        StringBuilder sb = new StringBuilder();
        sb.append("/merchant/").append(guid).append("/new_address");
        try {
            response = HttpClient.getInstance().post(API_SERVICE_URL, sb.toString(), paramMap);
        } catch (APIException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(response);
        return JacksonUtil.toStringMap(response);
    }

    /**
     * 创建一个比特币钱包地址
     */
    public static Address createAddress(String guid, String password, String label) {
        Wallet wallet = new Wallet(API_SERVICE_URL, API_CODE, guid, password);

        try {
            Address address = wallet.newAddress(label);
            return address;
        } catch (APIException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 创建一个比特币交易
     */
    public static PaymentResponse transaction(String toAddress, Long amount, String guid, String password) {
        Wallet wallet = new Wallet(API_SERVICE_URL, API_CODE, guid, password);
        PaymentResponse payment = null;
        try {

            payment = wallet.send(toAddress, amount, null, BITCOIN_FEE);
            System.out.println(String.format("The payment TX hash is %s", payment.getTxHash()));

        } catch (APIException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return payment;
    }


    /**
     * 查询账户余额
     */
    public static long getBitCoinBalance(String guid, String password) {
        Wallet wallet = new Wallet(API_SERVICE_URL, API_CODE, guid, password);
        try {
            long totalBalance = wallet.getBalance();
//            System.out.println(String.format("The total wallet balance is %s", totalBalance));
            return totalBalance;
        } catch (APIException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }

}
